package com.chapter.sample.redis.iomultiplex.bio.read.mthread;

import cn.hutool.core.util.IdUtil;

import java.io.IOException;
import java.io.InputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class RedisClientServerBIOMultiThread {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(6389);

        while (true) {
            System.out.println("---------RedisClientServerBIOMultiThread 111 等待连接");
            Socket socket = serverSocket.accept(); //阻塞1,等待客户端连接
            System.out.println("---------RedisClientServerBIOMultiThread 222 等待连接");

            new Thread(() -> {
                try {
                    InputStream inputStream = socket.getInputStream();
                    int length = -1;
                    byte[] bytes = new byte[1024];
                    System.out.println("---------333 等待读取 ：" + IdUtil.simpleUUID());
                    while ((length = inputStream.read(bytes)) != -1) { //阻塞2，等待客户端发送数据
                        System.out.println("----------444 读取成功" + new String(bytes, 0, length));
                        System.out.println("============== " + IdUtil.simpleUUID());
                        System.out.println();

                    }
                    inputStream.close();
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }, Thread.currentThread().getName()).start();

        }
    }
}
